package com.yanyilu.qiubite.controller;

import com.yanyilu.qiubite.entity.AdminUser;
import com.yanyilu.qiubite.entity.AdminUserSpot;
import com.yanyilu.qiubite.jwt.Audience;
import com.yanyilu.qiubite.jwt.JwtIgnore;
import com.yanyilu.qiubite.service.AdminUserService;
import com.yanyilu.qiubite.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("adminuser")
public class AdminUserController {
    @Autowired
    AdminUserService adminUserService;

    @Autowired
    Audience audience;

    /**
     * 管理员登录
     * @param adminUser
     * @return
     */
    @JwtIgnore
    @PostMapping("/admin/login")
    public R adminLogin(@RequestBody AdminUser adminUser){
        AdminUser user = adminUserService.login(adminUser);
        if(null != user){
            String token = JwtTokenUtil.createJWT(user.getId().toString(), user.getUserName(), user.getRole().toString(), audience);
            user.setPassword("不可查询");
            return R.ok().put("token",token).put("adminUser",user);
        }else{
            return R.error(ResultCode.USER_LOGIN_ERROR);
        }
    }

    /**
     * 管理员列表
     * @param page
     * @param limit
     * @return
     */
    ///qiubite/adminuser/admin/userlist
    @GetMapping("/admin/userlist")
    public R getAdminUserList(@RequestParam("page")Integer page,
                              @RequestParam("limit")Integer limit,
                              @RequestParam(value = "spotId",required = false)Integer spotId) throws Exception {
        String role = ParseJwtUtil.getRole();
        if("2".equals(role)){
            return R.ok(ResultCode.NO_ROLE_ERROR);
        }else{
            if("0".equals(role)){
                //若为超级管理员,且条件查询
                if(null != spotId){
                    Page<Object[]> pages = adminUserService.findUsersBySpotId(spotId,page,limit);
                    List<AdminUserSpot> list = ObjectToBean.objectToBean(pages.getContent(), AdminUserSpot.class);
                    return R.ok().put("list",list).put("totalPages", pages.getTotalPages()).put("count", pages.getTotalElements());
                }else{
                    Page<Object[]> pages = adminUserService.findAllUser(page,limit);
                    List<AdminUserSpot> list = ObjectToBean.objectToBean(pages.getContent(), AdminUserSpot.class);
                    return R.ok().put("list",list).put("totalPages", pages.getTotalPages()).put("count", pages.getTotalElements());
                }
            }else if("1".equals(role)){
                Integer userid = ParseJwtUtil.getUserId();
                AdminUser adminUser = adminUserService.findUserById(userid);
                if(null != adminUser){
                    Page<Object[]> pages = adminUserService.findUsersBySpotId(adminUser.getSpotId(),page,limit);
                    List<AdminUserSpot> list = ObjectToBean.objectToBean(pages.getContent(), AdminUserSpot.class);
                    return R.ok().put("list",list).put("totalPages", pages.getTotalPages()).put("count", pages.getTotalElements());
                }else{
                    return R.ok(ResultCode.NO_ROLE_ERROR);
                }
            }else{
                return R.ok(ResultCode.NO_ROLE_ERROR);
            }
        }
    }

    //qiubite/adminuser/user/update
    //修改账号密码
    @PostMapping("/user/update")
    public R updateUser(@RequestBody AdminUser adminuser){
        String role = ParseJwtUtil.getRole();
        
        if(null == adminuser.getUserName() || "".equals(adminuser.getUserName())){
            return R.error("账户不能为空");
        }
        if(null == adminuser.getPassword() || "".equals(adminuser.getPassword())){
            return R.error("密码不能为空");
        }
        if(adminUserService.findByUserName(adminuser.getUserName(),adminuser.getId())){
            return R.error("用户名已存在");
        }
        if("0".equals(role)){
            adminUserService.updateAdminUser(adminuser);
            return R.ok();
        }else{
            //Integer userid = ParseJwtUtil.getUserid();
            adminUserService.updateAdminUser1(adminuser);
            return R.ok();
        }
    }

    /**
     * 添加管理员
     * @param adminUser
     * @return
     */
    @PostMapping("/user/add")
    public R addUser(@RequestBody AdminUser adminUser){
        Integer userid = ParseJwtUtil.getUserId();
        String role = ParseJwtUtil.getRole();
        if("0".equals(role)){
            adminUserService.addUserFromAdmin(adminUser);
            return R.ok();
        }else if("1".equals(role)){
            if(adminUserService.addUser(adminUser,userid)){
                return R.ok();
            }else{
                return R.error();
            }
        }else{
            return R.error(ResultCode.NO_ROLE_ERROR);
        }
    }
}
